define(function(require, exports, module) {
    "use strict"; 
     var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config,
        method = app.method;
        require("./bankInput");
    var echarts = require("dist/echarts/init.js");
    var $flow_echarts=$("#flow_echarts"),$money_echarts = $("#money_echarts");

    


    $flow_echarts.length && (function(e) {
        var path = $flow_echarts.data("path");
        path && method.get(path, function(d) {
            if(config.issucceed(d)){
                echarts.line("flow_echarts", d.data)
            }else{
                config.msg.error(d.message);
            }
        })

    })(); 

    $money_echarts.length && (function(e) {
        var path = $money_echarts.data("path");
        path && method.get(path, function(d) {
            if(config.issucceed(d)){
                echarts.line("money_echarts", d.data)
            }else{
                config.msg.error(d.message);
            }
        })

    })(); 

    if($("#distribution").length){
        var $with_num = $("#with-num").val();
        var className = "add-box";
        $(document).on("change","#guidePriceP",function(){
            className = $(this).find("option:selected").data("select");
            $("span.error").remove();
            $("span.compare").addClass("hide");
            $("input.compare").addClass("hide").val("").removeClass('error').prop("disabled",true);
            $("."+className).removeClass("hide");
            $("input."+className).removeClass("hide").prop("disabled",false);

        });

        /*$(document).on("click","#submit",function(){        
            var vals = parseInt($("."+className).val());
            if(vals < 0 || $("."+className).val() == ""){
                return false;
            } 

            if(className == "reduce-box"){
                var oldNum = parseInt($("p.old-num").data("num"));
                var useNum = parseInt($("p.use-num").data("num"));
                var newNum = oldNum - useNum;
                if(vals > newNum){
                    return false; 
                }
            }
        })*/
    }

    $("#slect-grade").length && $(document).on("change","#slect-grade",function(){
        var data = JSON.parse($("#grade-val").val());
        console.log(data);
        var num = $(this).find("option:selected").data("grade");
        if(typeof num == "number"){
            $("input.grade-val").each(function(i){
                $(this).val(data[num][i]);
            })
        }else{
            $("input.grade-val").each(function(i){
                $(this).val("");
            })
        }
    })

    $("#button-sub").length && $(document).on("click","#button-sub",function(){
       if ($(this).closest("form").valid()) {   
            $("span.chang-phone").html($("#phoneNum").val());
            $("#js_sub_modal").modal("show");
       }
    })

    $("#sub-btn").length && $(document).on("click","#sub-btn",function(){
        $("form.form-validate").submit();
        $(this).button("loading");
        $("#js_sub_modal").modal("hide");
    })

    $("#bankCard").length && $("#bankCard").bankInput({max:30});
    


    $(document).on('click', '[data-toggle="ajaxPostPage"]', function(e) {
        e.preventDefault();
        var $this = $(this),
            $remote = $this.data('remote') || $this.attr('href'),
            $confirm = $this.data('confirm'),
            $msgType = $this.data('msgType'),
            $data = $this.data("set"),
            $remove = $this.data('remove'),
            $phoneNum = $("#phoneNum").val();
        $this.data("loadingText", '<i class="fa fa-spinner fa-spin"></i>')
        var done = function() {
            var org = $this.html();
            $this.button('loading');
            $.post($remote,{phoneNum:$phoneNum}).done(function(d) {
                $this.button('reset');
                if (config.issucceed(d)) {
                    if ($remove) {
                        var $el = $this.closest($remove);
                        $el.fadeOut(function() {
                            $el.remove();
                        });
                    };
                    config.msg.suc(d.message || config.lang.removeSuccess, d.url);
                    d.callback&&eval(d.callback);
                } else {
                    config.msg.error(d.message || config.lang.removeError, d.url)
                }
            }).fail(function() {
                $this.button('reset');
                config.msg.info(config.lang.exception)
            });

        }
        done();
    });
    
    $flow_echarts.length && (function(e) {
        var path = $flow_echarts.data("path");
        path && method.get(path, function(d) {
            if(config.issucceed(d)){
                echarts.line("flow_echarts", d.data)
            }else{
                config.msg.error(d.message);
            }
        })

    })(); 

    $(".js_opening").length && function(){
        var $opening=$(".js_opening");
        $opening.attr("disabled", "disabled");
        var max = 9000;
        $opening.text("10秒后确认开通")
        var t1 = setInterval(function() {
            $opening.text("{0}秒后确认开通".format(max / 1000))
            max -= 1000;
            if (max == 0) {
                $opening.text("确认开通").removeAttr('disabled')
                clearInterval(t1);
            };
        }, 1000)
    }();
    var $js_withdrawal_way=$(".js_withdrawal_way");
    $js_withdrawal_way.length&&(function(){
        var type=$js_withdrawal_way.find("option:selected").data("type");
        withdrawal(type)
    })();

    function withdrawal(type){
        $(".js_withdrawal_way_show").addClass("hide");
        $(".js_"+type).removeClass("hide");
    }
    $(document).on("change",".js_withdrawal_way",function(){
        var type=$(this).find("option:selected").data("type");
        withdrawal(type)
    });
    var $js_accounttype=$(".js_accounttype");
    $js_accounttype.length&&(function(){
        var atype=$(".js_accounttype:checked").data("type"),$bank=$(".js_account_bank"),
        bvalue=$bank.val(),btype=$bank.find("option:selected").data("type");
        accounttype(atype,bvalue,btype);
    })();
    $(document).on("click",".js_accounttype",function(){
        var atype=$(this).data("type"),$bank=$(".js_account_bank"),
        bvalue=$bank.val(),btype=$bank.find("option:selected").data("type");
        accounttype(atype,bvalue,btype);
    });
    $(document).on("change",".js_account_bank",function(){
        var atype=$(".js_accounttype:checked").data("type"),$bank=$(this),
        bvalue=$bank.val(),btype=$bank.find("option:selected").data("type");
        $(".js_subbank input").val("");
        accounttype(atype,bvalue,btype);
    });
    function accounttype(atype,bvalue,btype){
        var $js_subbank= $(".js_subbank"),$js_otherbank=$(".js_otherbank"),
        $line=$js_subbank.prev("div.line-dashed");
        if(atype=="public"){
            if(bvalue){
                $line.removeClass("hide");
                $js_subbank.removeClass("hide");
            }else{
                $line.addClass("hide");
                $js_subbank.addClass("hide");
            }
            if(btype=="other"){
                $js_otherbank.removeClass("hide");
            }else{
                $js_otherbank.addClass("hide");
            }
        }else if(atype=="private"){
            if(bvalue&&btype){
                $line.removeClass("hide");
                $js_subbank.removeClass("hide");
                if(btype=="other"){
                    $js_otherbank.removeClass("hide");
                }else{
                    $js_otherbank.addClass("hide");
                }
            }else{
               $line.addClass("hide");
               $js_subbank.addClass("hide");
               $js_otherbank.addClass("hide"); 
               $js_subbank.find("input").val("");
            }
        }
    }

    var $js_level_goods=$(".js_level_goods");
    $js_level_goods.length && (function(){
        var target=$(".js_level_goods:checked").data("target");
        levelBtn(target);
    })();
    $(document).on("click",".js_level_goods",function(){
        var target=$(this).data("target");
        levelBtn(target);
    });
    function levelBtn(btnClass){
        $(".js_goods_btn").addClass("hide");
        $(btnClass).removeClass("hide");
    }
    $(document).on("keyup","input[data-toggle='calculate']",function(){
        var $this=$(this),$taget=$($this.data("target")),price=$this.data("price"),
        type=$this.data("type"),result=$taget.val() || 0,val=$this.val() || 0,
        rang=$this.data("rang");
        if(type=="price"){
            if(rang!=undefined){
                result=(val*100/price[rang]).toFixed(2);
            }else{
                result=(val*100/price).toFixed(2);
            }
        }else{
            if(rang!=undefined){
                result=(price[rang]*val/100).toFixed(2);
            }else{
                result=(price*val/100).toFixed(2);
            }
            
        }
        $taget.val(result);
    });
    var $js_set_discount_dis=$(".js_set_discount_dis");
    $js_set_discount_dis.length && (function(){
        var target=$(".js_set_discount_dis:checked").data("target");
       coustom(target);
    })();
    $(document).on("click",".js_set_discount_dis",function(){
        var target=$(this).data("target");
        coustom(target);
    });
    function coustom(objClass){
         if(objClass){
            $(objClass).removeClass("hide");
        }else{
            $(".js_coustom_disc").addClass("hide");
        }
    }

    $(document).on("change",'[data-condition="change"]',function(){
        var data = $(this).data("val");
        console.log(data)
        if(data == 1){
            $(this).closest("div.form-group").next("div.form-group").find("span.open-inline").removeClass("hide");
            $(this).closest("div.form-group").next("div.form-group").find("span.close-inline").addClass("hide");
        }else{
            $(this).closest("div.form-group").next("div.form-group").find("span.open-inline").addClass("hide");
            $(this).closest("div.form-group").next("div.form-group").find("span.close-inline").removeClass("hide");
        }
    })
  });